/*
 * Copyright 2008 the original author Ang Gao.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * IRServiceImpl.java 2008-11-25
 */
package ie.ucc.cs.anggao.finalproject.server.service;

import ie.ucc.cs.anggao.finalproject.client.model.Query;
import ie.ucc.cs.anggao.finalproject.client.model.QueryEssay;
import ie.ucc.cs.anggao.finalproject.client.model.SearchEngineResult;
import ie.ucc.cs.anggao.finalproject.client.service.IRService;
import ie.ucc.cs.anggao.finalproject.server.AI.chunker;
import ie.ucc.cs.anggao.finalproject.server.searchengine.GoogleSearcher;
import ie.ucc.cs.anggao.finalproject.server.searchengine.YahooSearcher;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * @author GaoAng
 * 
 */
public class IRServiceImpl extends RemoteServiceServlet implements IRService {

	private ArrayList<SearchEngineResult> results;
	private GoogleSearcher googleSearcher;
	private YahooSearcher yahooSearcher;
	// private postagger POSTag;
	private chunker ChunkerNP;

	public IRServiceImpl() {
		super();
		results = new ArrayList<SearchEngineResult>();
		googleSearcher = new GoogleSearcher();
		yahooSearcher = new YahooSearcher();
		// After Experiment choice Chunker NP as Heuristic
		// POSTag = new postagger();
		ChunkerNP = new chunker();
	}

	public Query getSEquery(QueryEssay query) {

		HttpServletRequest request = this.getThreadLocalRequest();
		HttpSession session = request.getSession();
		
		String text = query.getHTMLContent();
		String q = query.getContent();
		System.out.println("Session before proecess : " +session.getAttribute("Text"));
		session.setAttribute("Text", text);
		
		if (!q.isEmpty()) {
			System.out.println("Query before proecess : " + q);
			Query newq = new Query(ChunkerNP.getNP(q));
			System.out.println("Query afer proecess : " + newq);
			System.out.println(newq.getQuery());
			return newq;
		}

		return null;

	}

	public List<SearchEngineResult> getSEresult(Query query) {
		String q = query.getQuery();
		ArrayList<SearchEngineResult> temp = new ArrayList<SearchEngineResult>();
		if (q != "") {
			// System.out.println("Query before proecess : " + q);
			Stack<String> stack = ChunkerNP.getNPs(q);
			// System.out.println("Query afer proecess : " + newq);

			results.clear();

			//
			// if (googleSearcher.doIR(newq) != null)
			// results.addAll(googleSearcher.doIR(newq));
			// if (yahooSearcher.doIR(newq) != null)
			// results.addAll(yahooSearcher.doIR(newq));
			Stack<String> tmp = (Stack<String>) stack.clone();
			String newq = "";
			for (int i = 0; i < 11; i++) {
				if (!tmp.isEmpty()) {
					newq = tmp.pop() + " " + newq;
				} else {
					break;
				}
			}

			googleSearcher.doIR(newq);
			yahooSearcher.doIR(newq);
			int number = 10;
			while (googleSearcher.getResultnumber()
					+ yahooSearcher.getResultnumber() < 10) {
				if (number == 0) {
					break;
				}
				Stack<String> stack2 = (Stack<String>) stack.clone();
				String modifyq = "";
				for (int i = 0; i < number; i++) {
					if (!stack2.isEmpty()) {
						newq = stack2.pop() + " " + modifyq;
					} else {
						break;
					}
				}
				number--;
				googleSearcher.doIR(modifyq);
				yahooSearcher.doIR(modifyq);
			}

			for (int i = 0; i < googleSearcher.getSEResults().size() - 1; i++) {
				for (int j = 0; j < yahooSearcher.getSEResults().size() - 1; j++) {
					if (yahooSearcher.getSEResults().get(j).getUrl().equals(googleSearcher.getSEResults().get(i).getUrl())) {
						yahooSearcher.getSEResults().remove(j);
						results.add(googleSearcher.getSEResults().get(i));
					}
				}
			}

//			results.addAll(googleSearcher.getSEResults());
			results.addAll(yahooSearcher.getSEResults());

			System.out.print("Transfor Total Results " + results.size());
			for (SearchEngineResult SEresult : results) {

				System.out.println("Transfor Result-------------"
						+ SEresult.getTitle());

			}

			return results;
		} else {
			return null;
		}

	}
}
